<?php

class Group extends DataObject
{
    // Declare data attributes
    protected $id;
    protected $groupname;
    protected $grouptype;



    public function fetchFromDB($id, $id2=null)
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        // Name of stored procedure
        $proc = 'GroupFetch';
        $params = array($id);
        if ($db->executeStoredProcedure($proc, $params, $returnData)){
            $this->id = intval($id);              
            $this->groupname = $returnData[0]["rolename"];
            $this->grouptype = intval($returnData[0]["rolename"]);

            // Stored procedure succeeded, return true
            return true;
        }
        // Stored procedure failed, return false
        return false;
    }
    
    public function commit()
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        $proc = 'GroupCommit';
        $params = Array(
            $this->id,
            $this->groupname
            );
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function delete()
    {
        $db = DBConnection::inst(); 
        $proc = 'GroupDelete';
        $params = array($this->id);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function create()
    {
        if (! isset($this->id))
        {
            // No database object stored.
            $db = DBConnection::inst();
            $proc = 'GroupCreate';
            $params = Array(
                $this->groupname,
                $this->grouptype
            );
            
            // Sets the ID to the return value of the stored procedure, which
            // is the ID of the newly created objects.
            if ($this->id = $db->executeStoredProcedure($proc, $params, $returnData))
            {
                return true;
            }
            return false;
        }
        // data object already set
        return false;
    }
    
    public function link()
    {
        return true;
    }
}

?>
